package com.mk.carcassonne;

import java.util.ArrayList;

import com.mk.carcassonne.drawables.Brick;
import com.mk.carcassonne.drawables.BrickEdgeType;

public class GameLogicUtils
{
	public static ArrayList<Position> getNeighbourPositions(Position position)
	{
		ArrayList<Position> positions = new ArrayList<Position>();
		positions.add(new Position(position.getX(), position.getY() - 1)); //above
		positions.add(new Position(position.getX() - 1, position.getY())); //left
		positions.add(new Position(position.getX() + 1, position.getY())); //right
		positions.add(new Position(position.getX(), position.getY() + 1)); //below
		
		return positions;
	}
	
	public static ArrayList<Brick> getBricksAtPositions(ArrayList<Position> positions, ArrayList<Brick> bricks) {
		ArrayList<Brick> result = new ArrayList<Brick>();
		for (Brick b : bricks) {
			if(positions.contains(b.getPosition()))
			{
				result.add(b);
			}
		}
		
		return result;
	}
	
	public static boolean areBricksCompatible(Brick comparerBrick, Brick newBrick, Position newPosition)
	{
		Integer offsetX = comparerBrick.getPosition().getX() - newPosition.getX();
		if(offsetX == -1) //comparer to the left
		{
			BrickEdgeType comparerBrickEdge = getR(comparerBrick);
			BrickEdgeType nextBrickEdge = getL(newBrick);
			if(comparerBrickEdge != nextBrickEdge){
				return false;	
			}				
		}
			
		if(offsetX == 1) //comparer to the right
		{
			BrickEdgeType brickEdge = getL(comparerBrick);
			BrickEdgeType nextBrickEdge = getR(newBrick);
			if(brickEdge != nextBrickEdge){
				return false;	
			}				
		}
		
		Integer offsetY = comparerBrick.getPosition().getY() - newPosition.getY();
		if(offsetY == -1) //comparer above
		{
			BrickEdgeType brickEdge = getD(comparerBrick);
			BrickEdgeType nextBrickEdge = getU(newBrick);
			if(brickEdge != nextBrickEdge){
				return false;	
			}				
		}
		
		if(offsetY == 1) //comparer under
		{
			BrickEdgeType brickEdge = getU(comparerBrick);
			BrickEdgeType nextBrickEdge = getD(newBrick);
			if(brickEdge != nextBrickEdge){
				return false;	
			}				
		}
		
		return true;
	}
	
	private static BrickEdgeType getU(Brick b) {
		switch (b.getRotation())
		{
			case 0:
				return b.getBrickEdgeTypeCollection().getUp();
			case 1:
				return b.getBrickEdgeTypeCollection().getLeft();
			case 2:
				return b.getBrickEdgeTypeCollection().getDown();
			default:
				return b.getBrickEdgeTypeCollection().getRight();
		}
	}
	
	private static BrickEdgeType getR(Brick b) {
		switch (b.getRotation())
		{
			case 0:
				return b.getBrickEdgeTypeCollection().getRight();
			case 1:
				return b.getBrickEdgeTypeCollection().getUp();
			case 2:
				return b.getBrickEdgeTypeCollection().getLeft();
			default:
				return b.getBrickEdgeTypeCollection().getDown();
		}
	}
	
	private static BrickEdgeType getD(Brick b) {
		switch (b.getRotation())
		{
			case 0:
				return b.getBrickEdgeTypeCollection().getDown();
			case 1:
				return b.getBrickEdgeTypeCollection().getRight();
			case 2:
				return b.getBrickEdgeTypeCollection().getUp();
			default:
				return b.getBrickEdgeTypeCollection().getLeft();
		}
	}
	
	private static BrickEdgeType getL(Brick b) {
		switch (b.getRotation())
		{
			case 0:
				return b.getBrickEdgeTypeCollection().getLeft();
			case 1:
				return b.getBrickEdgeTypeCollection().getDown();
			case 2:
				return b.getBrickEdgeTypeCollection().getRight();
			default:
				return b.getBrickEdgeTypeCollection().getUp();
		}
	}
}
